// pages/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTemperature: "N/A",
    WeatherDataGenerateDateTime: "loading...",
    location: 'loading...',
    weatherArray: [],
    listArray: [],
    provinceName: 'loading...',
    weatherInfo: 'loading...',
    LifeHelperAir: 'N/A',
    weatherIcon: "/images/icons/weather_icon_47.svg",
    LifeHelperWear: 'loading...',
    WeatherWindCondition: 'N/A',
    WeatherWindLevel: 'loading',
    LifeHelperUV: 'N/A'



  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    //页面加载完毕，获取用户的定位信息
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        // console.log(latitude, longitude)

        wx.request({
          url: 'https://geoapi.qweather.com/v2/city/lookup?location=' + res.longitude + ',' + res.latitude + '&key=	1989b40e90a2420f8969a4ed455dbf65',
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            console.log(res.data.location[0])
            that.setData({
              location: res.data.location[0].name,
              provinceName: res.data.location[0].adm2 + '/' + res.data.location[0].adm1 + '/' + res.data.location[0].country,
            });

            let code = '[' + res.data.location[0].lon + ',' + res.data.location[0].lat + ']';
            wx.request({
              url: 'https://api.gugudata.com/weather/weatherinfo?appkey=7AEU23XZUHZ5&code=' + code + '&days=7',
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(res) {
                // console.log(res.data.Data);


                that.setData({
                  weatherArray: that.remapData(res.data.Data),
                  WeatherDataGenerateDateTime: res.data.Data[0].WeatherDataGenerateDateTime,
                  currentTemperature: res.data.Data[0].TemperatureHigh,
                  LifeHelperAir: res.data.Data[0].LifeHelperAir.HelperStatus,
                  weatherIcon: that.getWeatherIcon(res.data.Data[0].WeatherInfo),
                  weatherInfo: res.data.Data[0].WeatherInfo,
                  LifeHelperWear: res.data.Data[0].LifeHelperWear.HelperContent.replace("。", ""),
                  WeatherWindCondition: res.data.Data[0].WeatherWindCondition2,
                  WeatherWindLevel: res.data.Data[0].WeatherWindLevel,
                  LifeHelperUV: res.data.Data[0].LifeHelperUV.HelperStatus

                })



              }
            })
          }
        })
      }
    })
  },

  remapData(data) {
    let listData = [];
    for (let i = 0; i < data.length; i++) {
      data[i].weekday = this.getWeekday(data[i].WeatherDataGenerateDateTime);
      data[i].icon = this.getWeatherIcon(data[i].WeatherInfo);
      data[i].date = data[i].WeatherDate.substring(5).replace("-", "月") + "日"
      if (i != 0) {
        listData.push(data[i])
      }
    }
    this.setData({
      listArray: listData,
    });

    return data;
  },

  getWeekday(date) {
    var mydate = new Date(date);
    var myddy = mydate.getDay();
    var weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    return weekday[myddy];
  },

  getWeatherIcon(weather) {
    switch (weather) {
      case "晴":
        return "/images/icons/weather_icon_4.svg";
      case "多云":
        return "/images/icons/weather_icon_3.svg";
      case "阴":
        return "/images/icons/weather_icon_2.svg";
      case "阵雨":
        return "/images/icons/weather_icon_8.svg";
      case "雷阵雨":
        return "/images/icons/weather_icon_23.svg";
      case "小雨":
        return "/images/icons/weather_icon_13.svg";
      case "中雨":
        return "/images/icons/weather_icon_7.svg";
      case "大雨":
        return "/images/icons/weather_icon_16.svg";
      case "阵雪":
        return "/images/icons/weather_icon_36.svg";
      case "小雪":
        return "/images/icons/weather_icon_35.svg";
      case "中雪":
        return "/images/icons/weather_icon_28.svg";
      case "大雪":
        return "/images/icons/weather_icon_28.svg";
      case "暴雪":
        return "/images/icons/weather_icon_28.svg";
      case "雾":
        return "/images/icons/weather_icon_30.svg";
      case "晴转多云":
        return "/images/icons/weather_icon_4.svg";
      case "多云转中雨":
        return "/images/icons/weather_icon_17.svg";
      case "多云转晴":
        return "/images/icons/weather_icon_3.svg";
      case "多云转阴":
        return "/images/icons/weather_icon_3.svg";
      case "中雨转多云":
        return "/images/icons/weather_icon_8.svg";
      case "晴转多云":
        return "/images/icons/weather_icon_3.svg";
      case "阴转多云":
        return "/images/icons/weather_icon_2.svg";
      case "阴转晴":
        return "/images/icons/weather_icon_2.svg";
      case "阴转小雨":
        return "/images/icons/weather_icon_2.svg";
      case "多云转小雨":
        return "/images/icons/weather_icon_14.svg";
      case "小雨转多云":
        return "/images/icons/weather_icon_13.svg";
      case "雷阵雨转多云":
        return "/images/icons/weather_icon_24.svg"
        break;
    }
  },

  showInfoPage: function () {
    let that = this;
    wx.navigateTo({
      url: '../info/info?location=' + that.data.location + '&weatherArray=' + JSON.stringify(that.data.weatherArray) + '&listArray=' + JSON.stringify(that.data.listArray) + '&provinceName=' + that.data.provinceName
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})